﻿// Copyright (c) 2010-2012 SharpDX - Alexandre Mutel
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;

using SharpDX;
using SharpDX.Toolkit;
using SharpDX.Toolkit.Graphics;
using Bikiball.Graphic;
using Bikiball.Graphic.Constructor;
using Bikiball.Graphic.Vextex;
using Bikiball.Game;
using BikiBall.Input;
using Bikiball.Core;
using Bikiball.Physic;
using System.Diagnostics;
using Bikiball.XLoader;

namespace Bikiball
{
    // Use this namespace here in case we need to use Direct3D11 namespace as well, as this
    // namespace will override the Direct3D11.


    /// <summary>
    /// Simple MiniCube application using SharpDX.Toolkit.
    /// The purpose of this application is to show a rotating cube using <see cref="BasicEffect"/>.
    /// </summary>
    public class BikiballGame : SharpDX.Toolkit.Game
    {
        private GraphicsDeviceManager graphicsDeviceManager;
        private GraphicEngine _graphicEngine;
        private GameWorld _gameWorld;
        private InputEngine _inputEngine;
        public Camera Camera;
        int i = 0;
        int fps = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="BikiballGame" /> class.
        /// </summary>
        public BikiballGame()
        {
            // Creates a graphics manager. This is mandatory.
            graphicsDeviceManager = new GraphicsDeviceManager(this);
            //graphicsDeviceManager.IsFullScreen = true;
            // Setup the relative directory to the executable directory
            // for loading contents with the ContentManager
            Content.RootDirectory = "Content";
            Camera = new Camera();
            //ModelData model = Content.Load<ModelData>("kirby.x");
        }

        protected override void LoadContent()
        {
            Parser parser = new Parser();
            _graphicEngine.LoadContent(Content);  
            //gp = Cube.Construct(1, 1, 1);
            this._gameWorld = new GameWorld();
            this._gameWorld.Map.SetProperties<GraphicProperty>(Map3DConstructor.Construct(this._gameWorld.Map));
            this._gameWorld.Map.SetProperties<MapGraphicProperty>(Map3DConstructor.ConstructOptimized(this._gameWorld.Map));
            this._inputEngine.Start();
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
#warning Penser à faire un dispose du monde
            this._inputEngine.Stop();
            base.UnloadContent();
        }

        protected override void Initialize()
        {
            Window.Title = "Bikiball demo";
            _graphicEngine = new GraphicEngine(GraphicsDevice);
            _graphicEngine.Init();
            _inputEngine = new InputEngine();
            this.TargetElapsedTime = TimeSpan.FromMilliseconds(10);
            base.Initialize();
        }

        protected override void Update(GameTime gameTime)
        {
            // Rotate the cube.
            var time = (float)gameTime.TotalGameTime.TotalSeconds;
            _inputEngine.Update();
            Camera.Position.X += _inputEngine.Pitch / 4;
            Camera.Position.Z += _inputEngine.Roll / 4;
            Camera.Position.Y = 1 * _inputEngine.PinchDelta;
            // Handle base.Update
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            i += gameTime.ElapsedGameTime.Milliseconds;
            fps++;
            if (i > 1000)
            {
                Debug.WriteLine(fps);
                fps = 0;
                i = 0;
            }
            // Clears the screen with the Color.CornflowerBlue
            _graphicEngine.RenderToDevice(_gameWorld.GameObjects, Camera, gameTime);
            // Handle base.Draw
            base.Draw(gameTime);
        }
    }
}
